﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;

namespace Accumulation.Controls.Menus {
    /// <summary>
    /// 菜单项集合
    /// </summary>
    public class MenuItemPlusCollection : CollectionBase, IStateManager {
        private List<MenuItemPlus> _list;
        private MenuItemPlus _owner;
        private bool _isTrackingViewState;
        /// <summary>
        /// 菜单项集合无参构造函数
        /// </summary>
        public MenuItemPlusCollection() : this(null) { }
        /// <summary>
        /// 菜单项集合构造函数
        /// </summary>
        /// <param name="owner">菜单项</param>
        public MenuItemPlusCollection(MenuItemPlus owner) {
            this._owner = owner;
            this._list = new List<MenuItemPlus>();
            this._isTrackingViewState = false;
        }
        /// <summary>
        /// 添加菜单项
        /// </summary>
        /// <param name="child">菜单项</param>
        public void Add(MenuItemPlus child) {
            this._list.Add(child);
        }
        /// <summary>
        /// 在指定位置添加菜单项
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="child">菜单项</param>
        public void AddAt(int index, MenuItemPlus child) {
            if (child == null) {
                throw new ArgumentNullException("child");
            }
            if (child.Owner != null && child.Parent != null) {
                child.Owner.Items.Remove(child);
            }
            if (child.Parent != null) {
                child.Parent.Items.Remove(child);
            }
            if (this._owner != null) {
                child.SetParent(this._owner);
                child.SetOwner(this._owner.Owner);
            }
            this._list.Insert(index, child);
        }
        /// <summary>
        /// 清除菜单项集合
        /// </summary>
        public new void Clear() {
            if (this.Count != 0) {
                if (this._owner != null) {
                    foreach (MenuItemPlus mip in this._list) {
                        mip.SetParent(null);
                    }
                }
                this._list.Clear();
            }
        }
        /// <summary>
        /// 菜单项集合是否包含指定的菜单项
        /// </summary>
        /// <param name="c">菜单项</param>
        /// <returns>bool</returns>
        public bool Contains(MenuItemPlus c) {
            return this._list.Contains(c);
        }
        /// <summary>
        /// 菜单项集合指定索引元素复制到数组对象
        /// </summary>
        /// <param name="array">数组对象</param>
        /// <param name="index">索引</param>
        public void CopyTo(Array array, int index) {
            if (!(array is MenuItemPlus[])) {
                throw new ArgumentException("数组类型错误");
            }
            this._list.CopyTo((MenuItemPlus[])array, index);
        }
        /// <summary>
        /// 菜单项集合指定索引元素复制到数组对象
        /// </summary>
        /// <param name="array">数组对象</param>
        /// <param name="index">索引</param>
        public void CopyTo(MenuItemPlus[] array, int index) {
            this._list.CopyTo(array, index);
        }
        /// <summary>
        /// 获取指定菜单项的索引
        /// </summary>
        /// <param name="value">菜单项</param>
        /// <returns>int</returns>
        public int IndexOf(MenuItemPlus value) {
            return this._list.IndexOf(value);
        }
        /// <summary>
        /// 移除指定的菜单项
        /// </summary>
        /// <param name="value">菜单项</param>
        public void Remove(MenuItemPlus value) {
            if (value == null) { throw new ArgumentNullException("对象不能为空"); }
            int index = this.IndexOf(value);
            if (index != -1) { this.RemoveAt(index); }
        }
        /// <summary>
        /// 移除指定索引的菜单项
        /// </summary>
        /// <param name="index">索引</param>
        public new void RemoveAt(int index) {
            MenuItemPlus mip = this._list[index];
            mip.SetParent(null);
            this._list.RemoveAt(index);
        }
        /// <summary>
        /// 获得指定索引的菜单项
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>MenuItemPlus</returns>
        public MenuItemPlus this[int index] {
            get { return this._list[index]; }
        }
        
        /// <summary>
        /// 菜单项数量
        /// </summary>
        public int Count {
            get { return this._list.Count; }
        }
        /// <summary>
        /// 是否可同步
        /// </summary>
        public bool IsSynchronized {
            get { return ((ICollection)this._list).IsSynchronized; }
        }
        /// <summary>
        /// 同步返回对象
        /// </summary>
        public object SyncRoot {
            get { return ((ICollection)this._list).SyncRoot; }
        }
        /// <summary>
        /// 迭代接口实现
        /// </summary>
        /// <returns>IEnumerator</returns>
        public new IEnumerator GetEnumerator() { return this._list.GetEnumerator(); }
        
        bool IStateManager.IsTrackingViewState {
            get { return this._isTrackingViewState; }
        }

        void IStateManager.LoadViewState(object state) {
            if (state != null) {
                object[] viewState = (object[])state;
                for (int i = 0; i < viewState.Length; i++) {
                    ((IStateManager)this._list[i]).LoadViewState(viewState[i]);
                }
            }
        }

        object IStateManager.SaveViewState() {
            object[] iState = new object[this._list.Count];
            for (int i = 0; i < this._list.Count; i++) {
                iState[i] = ((IStateManager)this._list[i]).SaveViewState();
            }
            return iState;
        }

        void IStateManager.TrackViewState() {
            for (int i = 0; i < this._list.Count; i++) {
                ((IStateManager)this._list[i]).TrackViewState();
            }
        }
    }
}
